package com.swlz.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.swlz.commons.DBUtil;
import com.swlz.entity.MallAddress;

import java.util.Date;
/**
 * <pre>
 * 数据访问对象
 * 商城用户收货地址
 * TableName : MALL_ADDRESS
 * ClassName : com.swlz.dao.MallAddressDAO
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-29 20:34:27
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class MallAddressDAO {

	/**
	 * 添加一个商城用户收货地址
	 * 包含所有字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(MallAddress mallAddress) throws SQLException {
		if(mallAddress.getAddressId()==null) {
			mallAddress.setAddressId(DBUtil.nextId());
		}
		return DBUtil.insert(mallAddress);
	}
 
	
	/**
	 * 添加一个商城用户收货地址
	 * 只包含非null的字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insertSelective(MallAddress mallAddress) throws SQLException {
		if(mallAddress.getAddressId()==null) {
			mallAddress.setAddressId(DBUtil.nextId());
		}
		return DBUtil.insertSelective(mallAddress);
	}
	

	/**
	 * 根据主键更新一个商城用户收货地址
	 * 包含所有字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int update(MallAddress mallAddress) throws SQLException {
		return DBUtil.update(mallAddress);
	}
	
	/**
	 * 根据主键更新一个商城用户收货地址
	 * 只包含非null的字段
	 * @param mallAddress - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateSelective(MallAddress mallAddress) throws SQLException {
		return DBUtil.updateSelective(mallAddress);
	}
	
	/**
	 * 根据主键删除一个商城用户收货地址
	 * @param addressId - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteById(Long addressId) throws SQLException  {
		return DBUtil.execute("DELETE FROM MALL_ADDRESS WHERE ADDRESS_ID=?", addressId);
	}
	
	/**
	 * 根据主键获取一个商城用户收货地址
	 * @param addressId - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallAddress selectById(Long addressId) throws SQLException  {
		return DBUtil.get("SELECT * FROM MALL_ADDRESS WHERE ADDRESS_ID=?", MallAddress.class ,addressId);
	}

		
	/**
	 * 根据模型中的非空字段查询
	 * @param addressId - 实体对象
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public MallAddress selectByModel(MallAddress mallAddress)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ADDRESS WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		return DBUtil.get(sql.toString(), MallAddress.class, params.toArray());
	}
	
	/**
	 * 根据模型中的非空字段查询
	 * @param addressId - 实体对象
	 * @param index - 开始索引
	 * @param len - 返回长度
	 * @return
	 * @throws SQLException
	 */
	public List<MallAddress> selectByModel(MallAddress mallAddress,int index,int len)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT * FROM MALL_ADDRESS WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		sql.append(" ORDER BY ADDRESS_ID DESC LIMIT ?,?");
		params.add(index);
		params.add(len);
		return DBUtil.list(sql.toString(), MallAddress.class, params.toArray());
	}

	/**
	 * 根据模型中的非空字段统计
	 * @param addressId - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long countByModel(MallAddress mallAddress)throws SQLException {
		StringBuffer sql = new StringBuffer("SELECT count(*) as c FROM MALL_ADDRESS WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		where(mallAddress,sql,params);
		return DBUtil.getLong(sql.toString(), params.toArray());
	}
	
	/**
	 * 格式化where条件和参数
	 * @param addressId - 实体对象
	 * @param sql - SQL语句
	 * @param params - SQL语句中的参数
	 */
	private void where(MallAddress mallAddress,StringBuffer sql,List<Object> params) {
		if(mallAddress==null){
			return;
		}

		if(mallAddress.getAddressId()!=null) {
			sql.append(" AND ADDRESS_ID=?");
			params.add(mallAddress.getAddressId());
		}

		if(mallAddress.getUserId()!=null) {
			sql.append(" AND USER_ID=?");
			params.add(mallAddress.getUserId());
		}

		if(mallAddress.getAddressName()!=null) {
			sql.append(" AND ADDRESS_NAME=?");
			params.add(mallAddress.getAddressName());
		}

		if(mallAddress.getAddressTel()!=null) {
			sql.append(" AND ADDRESS_TEL=?");
			params.add(mallAddress.getAddressTel());
		}

		if(mallAddress.getAddressZip()!=null) {
			sql.append(" AND ADDRESS_ZIP=?");
			params.add(mallAddress.getAddressZip());
		}

		if(mallAddress.getAddressCode()!=null) {
			sql.append(" AND ADDRESS_CODE=?");
			params.add(mallAddress.getAddressCode());
		}

		if(mallAddress.getAddressInfo()!=null) {
			sql.append(" AND ADDRESS_INFO=?");
			params.add(mallAddress.getAddressInfo());
		}

		if(mallAddress.getAddressDefault()!=null) {
			sql.append(" AND ADDRESS_DEFAULT=?");
			params.add(mallAddress.getAddressDefault());
		}

	}
}